package com.cqc.deviceanimreceiver01;

import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Context context = this;
    private ComponentName componentName;
    private DevicePolicyManager dpm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        componentName = new ComponentName(context, MyDeviceAdminReceiver.class);
    }


    public void click(View view) {
        switch (view.getId()) {
            case R.id.btn_active://激活超级管理员权限 设置->安全->设备管理器
                //如果没有激活权限，提示激活
                if (!dpm.isAdminActive(componentName)) {
                    Toast.makeText(context, "正在激活", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
                    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Admin");
                    startActivity(intent);
                } else {
                    Toast.makeText(context, "已经激活", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_inActive:
                dpm.removeActiveAdmin(componentName);//取消激活
                break;
            case R.id.btn_lock:
                dpm.resetPassword("123", DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);
                dpm.lockNow();
                break;
            case R.id.btn_clear:
                dpm.wipeData(0);
//                dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//清除SD卡数据
//                dpm.wipeData(DevicePolicyManager.WIPE_RESET_PROTECTION_DATA);//清除保护数据
                break;
        }
    }
}
